package com.sm.vod;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.sm.vo.VodVO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * VOD工具类
 */
public class AliyunUpload {

    //分享动态转码组
    private static final String TEMPLATE_GROUPID = "3b6052a3178cb063acdec9d4b4475bcd";
    private static String regionId = "cn-shanghai";
    private static String accessKeyId = "LTAI4GJYDT8npgCEX17bza3A";
    private static String accessKeySecret = "sJrHLJ6jKct2nxOHrhHeVFbUpEMAvm";

    //初始化客户端
    public static DefaultAcsClient initVodClient() {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        return new DefaultAcsClient(profile);
    }


    /*获取播放地址函数*/
    public static VodVO getPlayInfo(String videoId) throws Exception {
        DefaultAcsClient client = initVodClient();
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        GetPlayInfoResponse acsResponse = client.getAcsResponse(request);
        VodVO vodVO = new VodVO();
        vodVO.setCoverUrl(acsResponse.getVideoBase().getCoverURL());
        vodVO.setTitle(acsResponse.getVideoBase().getTitle());
        List<GetPlayInfoResponse.PlayInfo> playInfoList = acsResponse.getPlayInfoList();
        vodVO.setLdUrl(playInfoList.get(0).getPlayURL());
        if (playInfoList.size() > 1) {
            vodVO.setSdUrl(playInfoList.get(1).getPlayURL());
            vodVO.setHdUrl(playInfoList.get(2).getPlayURL());
        }
        return vodVO;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(getPlayInfo("8f0add67637b4622916b145b35c7a80a"));
    }


    /*获取播放凭证函数*/
    public static Map<String, Object> getVideoPlayAuth(String videoId) {
        DefaultAcsClient client = initVodClient();
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        request.setAuthInfoTimeout(3000L);
        Map<String, Object> map = new HashMap<>();
        try {
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            map.put("Code", "0");
            map.put("PlayAuth", response.getPlayAuth());
            map.put("RequestId", response.getRequestId());
            map.put("VideoId", response.getVideoMeta().getVideoId());
            map.put("Title", response.getVideoMeta().getTitle());
            map.put("CoverUrl", response.getVideoMeta().getCoverURL());
        } catch (ClientException e) {
            e.printStackTrace();
            map.put("Code", "1");
            map.put("ErrorMessage", e.getLocalizedMessage());
        }
        return map;
    }


    /**
     * 获取视频上传地址和凭证
     */
    public static HashMap<String, Object> createUploadVideo(Map<String, Object> param) {
        DefaultAcsClient client = initVodClient();

        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle(param.get("title").toString());
        request.setFileName(param.get("fileName").toString());
        request.setCateId(1000140164L);
        request.setTemplateGroupId(TEMPLATE_GROUPID);

        HashMap<String, Object> map = new HashMap<>();
        try {
            CreateUploadVideoResponse response = client.getAcsResponse(request);
            map.put("Code", "0");
            map.put("VideoId", response.getVideoId());
            map.put("UploadAddress", response.getUploadAddress());
            map.put("UploadAuth", response.getUploadAuth());
            map.put("RequestId", response.getRequestId());
        } catch (ClientException e) {
            e.printStackTrace();
            map.put("Code", "1");
            map.put("ErrorMessage", e.getLocalizedMessage());
        }

        return map;
    }

    /**
     * 刷新视频上传凭证
     */
    public static Map<String, Object> refreshUploadVideo(String videoId) {
        DefaultAcsClient client = initVodClient();
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        request.setVideoId(videoId);

        Map<String, Object> map = new HashMap<>();
        try {
            RefreshUploadVideoResponse response = client.getAcsResponse(request);
            map.put("Code", "0");
            map.put("VideoId", response.getVideoId());
            map.put("UploadAddress", response.getUploadAddress());
            map.put("UploadAuth", response.getUploadAuth());
        } catch (ClientException e) {
            e.printStackTrace();
            map.put("Code", "1");
            map.put("ErrorMessage", e.getLocalizedMessage());
        }
        return map;
    }

    /**
     * 获取图片上传地址和凭证
     *
     * @param title 标题
     * @return CreateUploadImageResponse 获取图片上传地址和凭证响应数据
     * @throws Exception
     */
    public static Map<String, Object> getImgUploadImage(String title) {
        DefaultAcsClient client = initVodClient();
        CreateUploadImageRequest request = new CreateUploadImageRequest();
        request.setImageType("default");
        request.setImageExt("png");
        request.setTitle(title);


        Map<String, Object> map = new HashMap<>();
        try {
            CreateUploadImageResponse response = client.getAcsResponse(request);
            map.put("Code", "0");
            map.put("ImageId", response.getImageId());
            map.put("ImageURL", response.getImageURL());
            map.put("UploadAddress", response.getUploadAddress());
            map.put("UploadAuth", response.getUploadAuth());
        } catch (ClientException e) {
            e.printStackTrace();
            map.put("Code", "1");
            map.put("ErrorMessage", e.getLocalizedMessage());
        }
        return map;
    }


}



